package com.bzy.blp.service.impl;

import com.bzy.blp.model.TPile;
import com.bzy.blp.view.DrillingDetailView;
import com.bzy.blp.view.ShowPileDetailView;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.util.List;
import java.util.List;

@Service
public class CreatePileWordProcessor {

    public void createPileWord(String path, ShowPileDetailView showPileDetailView) throws IOException, DocumentException {
        //设置纸张大小
        Document document = new Document(PageSize.A4);
        //建立一个书写器，与document对象关联
        RtfWriter2.getInstance(document, new FileOutputStream(path));
        document.open();
        //设置中文字体
        BaseFont bfChinese = BaseFont.createFont("C:/windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//	        //标题字体风格
//	        Font titleFont = new Font(bfChinese,14,Font.BOLD);
        //正文字体风格
        Font contextFont = new Font(bfChinese, 10, Font.BOLD);
//	        Paragraph title = new Paragraph("标题");
//	        //设置标题格式对齐方式
//	        title.setAlignment(Element.ALIGN_CENTER);
//	        title.setFont(titleFont);
//	        document.add(title);
        Paragraph context = new Paragraph("");
        context.setAlignment(Element.ALIGN_LEFT);
        context.setFont(contextFont);
        //段间距
        context.setSpacingBefore(3);
        //设置第一行空的列数
        context.setFirstLineIndent(20);
        document.add(context);
        //设置Table表格,创建一个三列的表格
        Font fontChinese = new Font(bfChinese, 12, Font.BOLD, Color.DARK_GRAY);

        TPile tPile = showPileDetailView.gettPile();
        List<DrillingDetailView> tDrillingDetails = showPileDetailView.gettDrillingDetails();


        Table table = new Table(9);
        int width[] = {10, 12, 15, 12, 15, 10, 10, 10, 10};//设置每列宽度比例
        table.setWidths(width);
        table.setWidth(100);//占页面宽度比例
        table.setAlignment(Element.ALIGN_CENTER);//居中
        table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
        table.setAutoFillEmptyCells(true);//自动填满
        table.setBorderWidth(10);//边框宽度
        Cell cell;
        //设置表头
        cell = new Cell(new Paragraph("桩号", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);


        cell = new Cell(new Paragraph(tPile.getPileNumber(), fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new Cell(new Paragraph("桩径(mm)", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new Cell(new Paragraph(tPile.getPileDiameter() == null ? "" : tPile.getPileDiameter().toString(), fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new Cell(new Paragraph("强度(MPa)", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new Cell(new Paragraph(tPile.getConcreteDesignGrade() == null ? "" : tPile.getConcreteDesignGrade().toString(), fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new Cell(new Paragraph("判定", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new Cell(new Paragraph(tPile.getStatus() == 1 ? "满足设计要求" : "不满足设计要求", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        cell.setColspan(2);
        table.addCell(cell);

        cell = new Cell(new Paragraph("设计等级", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        String designGrade = "";
        switch (tPile.getConcreteDesignGrade()) {
            case 0:
                designGrade = "C30";
                break;
            case 1:
                designGrade = "C40";
                break;
            case 2:
                designGrade = "C50";
                break;
            case 3:
                designGrade = "C60";
                break;
            case 4:
                designGrade = "C70";
                break;
        }
        cell = new Cell(new Paragraph(designGrade, fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);


        cell = new Cell(new Paragraph("桩长(m)", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new Cell(new Paragraph(tPile.getcPileLength() == null ? "" : tPile.getcPileLength().toString(), fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new Cell(new Paragraph("桩身完整性", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        cell.setColspan(2);
        table.addCell(cell);

        String pileIntegrity = "";
        switch (tPile.getdPileIntegrity()) {
            case 1:
                pileIntegrity = "I";
                break;
            case 2:
                pileIntegrity = "II";
                break;
            case 3:
                pileIntegrity = "III";
                break;
            case 4:
                pileIntegrity = "IV";
                break;
        }
        cell = new Cell(new Paragraph(pileIntegrity, fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new Cell(new Paragraph("沉渣", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new Cell(new Paragraph(tPile.getSedimentThicknessStatus() == 1 ? "无" : "有", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        for (DrillingDetailView drillingDetailView : tDrillingDetails) {
            cell = new Cell(new Paragraph("钻孔号", fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            table.addCell(cell);

            cell = new Cell(new Paragraph(drillingDetailView.getDrillingNo(), fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            cell.setColspan(3);
            table.addCell(cell);

            Image gif = Image.getInstance(new URL(drillingDetailView.getCordImg()));
            gif.scaleAbsolute(260f, 180);
            cell = new Cell(gif);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            cell.setColspan(5);
            cell.setRowspan(12);
            table.addCell(cell);

            cell = new Cell(new Paragraph("桩身混凝土", fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            cell.setRowspan(5);
            table.addCell(cell);

            cell = new Cell(new Paragraph("长度(m)", fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            cell.setColspan(2);
            table.addCell(cell);

            cell = new Cell(new Paragraph(drillingDetailView.getConcreteLength() == null ? "" : drillingDetailView.getConcreteLength().toString(), fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            table.addCell(cell);

            cell = new Cell(new Paragraph(tPile.getDescriptionPileConcreteDescription(), fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            cell.setRowspan(4);
            cell.setColspan(3);
            table.addCell(cell);

            cell = new Cell(new Paragraph("沉渣", fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            cell.setColspan(2);
            table.addCell(cell);

            cell = new Cell(new Paragraph(drillingDetailView.getSedimentThickness().compareTo(BigDecimal.ZERO) == 0 ? "无" : "有", fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            cell.setColspan(2);
            table.addCell(cell);

            cell = new Cell(new Paragraph("持力层", fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            cell.setRowspan(5);
            table.addCell(cell);

            cell = new Cell(new Paragraph("检测深度(m)", fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            cell.setColspan(2);
            table.addCell(cell);

            cell = new Cell(new Paragraph(drillingDetailView.getDrillingDepthBearingStratum() == null ? "" : drillingDetailView.getDrillingDepthBearingStratum().toString(), fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            table.addCell(cell);

            cell = new Cell(new Paragraph(tPile.getBearingLayerDescription(), fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
            cell.setRowspan(4);
            cell.setColspan(3);
            table.addCell(cell);


        }

        document.add(table);
        document.close();
    }

}
